package com.csh.szhm.custorm.autoviewpager;

import java.util.ArrayList;
import java.util.List;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageLoader.ImageCache;
import com.csh.szhm.SzhmApplication;
import com.csh.szhm.http.HttpUrls;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.LruCache;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

/**
 * ImagePagerAdapter
 * 
 * @author <a href="http://www.trinea.cn" target="_blank">Trinea</a> 2014-2-23
 */
public class ImagePagerAdapter<T> extends RecyclingPagerAdapter implements
		OnClickListener {

	private Context context;
	private List<T> imageIdList = new ArrayList<T>();
	private int size;
	private boolean isInfiniteLoop;
	private ImageLoader mImageLoader;

	public ImagePagerAdapter(Context context) {
		this.context = context;
		isInfiniteLoop = false;
		mImageLoader = new ImageLoader(SzhmApplication.getRequestQueue(),
				new BitmapCache());
	}

	/**
	 * 添加资源文件
	 * 
	 * @param imageIdList
	 */
	public void addData(List<T> imageIdList) {
		if (imageIdList != null) {
			this.imageIdList.addAll(imageIdList);
			this.size = imageIdList.size();
			notifyDataSetChanged();
		}
	}

	public void clear() {
		this.imageIdList.clear();
		this.size = 0;
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		// Infinite loop
		if (imageIdList.size() > 0) {
			return isInfiniteLoop ? Integer.MAX_VALUE : size;
		}
		return 0;
	}

	/**
	 * get really position
	 * 
	 * @param position
	 * @return
	 */
	private int getPosition(int position) {
		if (size == 0) {
			return 0;
		}
		return isInfiniteLoop ? position % size : position;
	}

	@Override
	public View getView(final int position, View view, ViewGroup container) {
		ViewHolder holder;
		if (view == null) {
			holder = new ViewHolder();
			view = holder.imageView = new ImageView(context);
			holder.imageView.setScaleType(ScaleType.FIT_XY);
			holder.imageView.setOnClickListener(this);
			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}
		final T item = imageIdList.get(getPosition(position));
		if (item instanceof Integer) {
			Integer temp = (Integer) item;
			holder.imageView.setImageResource(temp);
		} else if (item instanceof String) {
			String temp = (String) item;
			SzhmApplication.displayUserDefalue(HttpUrls.PATH_ROOT + temp,
					holder.imageView);
		} else {
			// AdListBean temp = (AdListBean) item;
			// holder.bean = temp;
			// JHouseApplication.display(HttpUrls.PATH_ROOT + temp.getBanner(),
			// holder.imageView);
		}

		return view;
	}

	public T getItemData(int postion) {
		return imageIdList.get(postion);
	}

	private static class ViewHolder {

		ImageView imageView;
		// AdListBean bean;
	}

	/**
	 * @return the isInfiniteLoop
	 */
	public boolean isInfiniteLoop() {
		return isInfiniteLoop;
	}

	/**
	 * @param isInfiniteLoop
	 *            the isInfiniteLoop to set
	 */
	public void setInfiniteLoop(boolean isInfiniteLoop) {
		this.isInfiniteLoop = isInfiniteLoop;
	}

	@Override
	public void onClick(View v) {
		// ViewHolder holder = (ViewHolder) v.getTag();
		// AppConfig.adStartActivity(holder.bean.getType(),
		// holder.bean.getParam(), context);
	}

	public class BitmapCache implements ImageCache {
		private LruCache<String, Bitmap> mCache;

		public BitmapCache() {
			int maxSize = 10 * 1024 * 1024;
			mCache = new LruCache<String, Bitmap>(maxSize) {
				@Override
				protected int sizeOf(String key, Bitmap value) {
					return value.getRowBytes() * value.getHeight();
				}

			};
		}

		@Override
		public Bitmap getBitmap(String url) {
			return mCache.get(url);
		}

		@Override
		public void putBitmap(String url, Bitmap bitmap) {
			mCache.put(url, bitmap);
		}
	}
}